package cn.numeron.common.utility

import cn.numeron.common.extension.parse
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import java.lang.reflect.Type
import java.util.*

class DateDeserializer : JsonDeserializer<Date> {

    override fun deserialize(
        json: JsonElement,
        typeOfT: Type,
        context: JsonDeserializationContext
    ): Date {
        val value = json.asJsonPrimitive
        return when {
            value.isNumber -> Date(value.asLong)
            value.isString -> value.asString.parse()
            else -> throw IllegalStateException()
        }
    }

}